<template>
  <div class="preOrder">
    <h1 :style="{ textAlign: 'left' }">确认订单</h1>
    <el-table style="width: 100%" :data="proArr">
      <el-table-column label="充值产品">
        <template v-slot="{ row }">
          <img :src="row.imageUrl" width="80px" height="30px" />
          <br />
          {{ row.description }}
        </template>
      </el-table-column>
      <el-table-column prop="price" label="单价"> </el-table-column>
      <el-table-column label="数量">
        <template v-slot="{ row }">
          <el-input-number v-model="num" :min="1" :max="10"> </el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="小计">{{ allPrice }}元</el-table-column>
    </el-table>

    <div class="tipbox">
      <p>
        实付款：<span :style="{ color: 'red' }">￥{{ allPrice }}元</span>
      </p>
      <p>
        充值详情：<span :style="{ color: 'gold', fontWeight: 'bold' }">{{
          proObj.description
        }}</span>
      </p>
      <p>充值账户</p>
    </div>
    <button class="btn" @click="submitOrder">提交订单</button>
  </div>
</template>

<script>

import { mapState } from "vuex";
import * as payApi from '@/api/pay'
export default {
  data() {
    return {
      num: 1,
      order_id:''
    };
  },
  computed: {
    ...mapState("preOrder", ["proObj"]),
    proArr() {
      return [this.proObj];
    },
    // 定义计算属性，算出总价
    allPrice() {
      return (this.num * this.proObj.price).toFixed(2);
    },
  },
  async mounted() {
    let res = await payApi.placeAnOrder(this.proObj.product_id)
    this.order_id = res.data.order_id
    
  },

  methods: {
    submitOrder() {
      // 当点击按钮，触发此事件，这是整个支付流程中最关键的一步
      // 点击按钮时也会弹出提示框提示是否确认购买，如果点了确定
      // 购买就需要做如下几件事：
      //           1.请求接口，同时传递订单号
      //           2.跳转到支付界面
      this.$confirm("是否确认？", "您正在提交订单", {
        type: "success",
      })
      .then(() => {
        // 如果点击了确定了走这个回调
        // 发起请求,需要传入订单id
        payApi.sendPay(this.order_id)
        .then(res=>{
          
          // 得到code_url，需要传递给下个路由，生成二维码图片
          let code_url = res.data.code_url
          this.$router.push({name:"home-PayMent-Pay"})
          this.$store.commit('preOrder/GETORDER_ID',this.order_id)
          this.$store.commit('preOrder/GET_CODE_URL',code_url)
          console.log(this.$router);
        })
      
      })
      .catch(() => {
        this.$message.error('已取消')
      })


    },
  },
};
</script>

<style scoped lang='less'>
.btn {
  border: none;
  color: rgb(254, 241, 0);
  width: 200px;
  height: 50px;
  background: rgb(152, 11, 34);
  position: absolute;
  right: 50px;
  top: 600px;
}
.tipbox {
  width: 500px;
  height: 150px;
  border-radius: 10px;
  border-left: 1px solid rgb(134, 18, 80);
  border-top: 1px solid rgb(134, 18, 80);
  box-shadow: 10px 10px 10px rgb(171, 38, 60);
  display: flex;
  flex-direction: column;
  position: absolute;
  right: 50px;
  top: 450px;
  justify-content: left;
  align-items: left;
  p {
    height: 50px;
    line-height: 50px;
    width: 200px;
    text-align: left;
    padding-left: 15px;
    overflow: hidden;
  }
}
.tipbox p:nth-of-type(2) {
  width: 500px;
}
/deep/.el-table__header-wrapper {
  line-height: 50px;
  text-align: center;
}
/deep/.el-table__row td .cell {
  text-align: center;
}
/deep/.el-table__header .has-gutter tr th {
  text-align: center;
}
/deep/.el-table__row td:nth-of-type(2) .cell {
  color: red !important;
  font-weight: bolder;
  font-size: 16px;
}
/deep/.el-table__row td:nth-of-type(4) .cell {
  color: red !important;
  font-weight: bolder;
  font-size: 16px;
}
</style>
